<?php 
    /**
     *  This file outputs cart
     * 
     *  Его можно вызывать через AJAX, при этом передавая такие данные:
     *  id_product_delete - Продукт с таким ID будет удален из корзины
     *  id_product_change_quantity - У продукта с таким ID будет изменено количество на product_change_quantity
     *  product_change_quantity - Новое количесто для продукта, заданного в id_product_change_quantity
     * 
     */     
    require_once('../../includes/spip_launcher.php');
    include_spip('functions/pdk_common');
    //include_spip('classes/alohacart');
    $cart = pdk_load_class('Alohacart','classes');
    $product_mdl = pdk_load_class('ProductModel','classes');
    $currency_mdl = pdk_load_class('CurrencyModel','classes');
    // If we have product to delete - delete it
    $id_product_delete = intval(_request('id_product_delete'));
    if ($id_product_delete)
        $cart->RemoveProduct($id_product_delete);
    
    // If we have product with changed quantity - change it
    $id_product_change_quantity = intval(_request('id_product_change_quantity'));
    $product_change_quantity = intval(_request('product_change_quantity'));
    $not_enough_products_script = "";
    if ($id_product_change_quantity) {
        $product = $product_mdl->select_by_id($id_product_change_quantity);    
        if ($product_change_quantity > $product['quantity'] && lire_config('alohacart/config_consider_quantity')) {
            $not_enough_products_script = "<script>show_dialog('not_enough_products');</script>";
            $cart->UpdateProduct($id_product_change_quantity, $product['quantity']);
        } else
            $cart->UpdateProduct($id_product_change_quantity, $product_change_quantity);
    }
    
    // Define some data. This code is similar to code in charger function
    $_REQUEST['products_ids'] = $cart->GetProductsIDs();
    $_REQUEST['products'] = $cart->GetProducts();    
    // Calculate prices for products according to quantities
    $_REQUEST['products_prices'] = $cart->GetProductsTotals();
    
    // Order total price
    $_REQUEST['order_total_price'] = $cart->CountPrice();
    
    // Shipping cost
    if ($id_shipping = intval($_REQUEST['id_shipping'])) {
        $extension_mdl = pdk_load_class('AlohacartExtensionModel','classes');
        $shipping_extension = $extension_mdl->GetExtensionClassByID($id_shipping);
        $shipping_cost = $currency_mdl->ConvertPriceToCurrentCurrency($shipping_extension->GetCost());
        if ($shipping_cost) {
            $_REQUEST['shipping_cost'] = $shipping_cost; 
            $_REQUEST['order_total_price'] += $shipping_cost;
        } else {
            $_REQUEST['shipping_cost'] = '';
            // If no shipping cost - don't output order total price 
            $_REQUEST['order_total_price'] = '';
        }
            
    } else {
		// If no shipping - don't output order total price
		$_REQUEST['order_total_price'] = '';
	}
    
    if (($id_composite = $_REQUEST['id_composite']) && ($id_composite_article = $_REQUEST['id_composite_article']) &&
        ($composite_quantity = $_REQUEST['composite_quantity'])) {
            //var_dump($id_composite, $id_composite_article, $composite_quantity); exit;
        $cart->UpdateCompositeProduct($id_composite, $id_composite_article, $composite_quantity);
    }
    
    // Output cart's template:
    echo recuperer_fond('formulaires/alohacart_cart_order/cart', $_REQUEST).$not_enough_products_script;
?>
